package it.webrules.deploymanager.core.st.scm;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LogEntry {

	public String author;
	public Date date;
	public String message;
	public long revision;
	public Map<String, String> revisionProperties;
	public List<LogEntryChangedPath> changedPath;

	public LogEntry() {
	}

	public LogEntry(String author, Date date, String message, long revision) {
		super();
		this.author = author;
		this.date = date;
		this.message = message;
		this.revision = revision;
	}

	public LogEntry addChangedPath(LogEntryChangedPath logEntryChangedPath) {
		if (changedPath == null)
			changedPath = new ArrayList<LogEntryChangedPath>();
		changedPath.add(logEntryChangedPath);
		return this;
	}

	public LogEntry addRevisionProperties(String name, String value) {
		if (revisionProperties == null)
			revisionProperties = new HashMap<String, String>();
		revisionProperties.put(name, value);
		return this;
	}

	@Override
	public String toString() {

		Formatter formatter = new Formatter();
		SimpleDateFormat sdf = new SimpleDateFormat();
		String sdate = sdf.format(date);

		formatter.format("%-8s | %-15s | %15s | %-55s", revision, author, sdate, message);

		if (changedPath != null)
			for (LogEntryChangedPath logEntryChangedPath : changedPath) {
				formatter.format("\n%s", logEntryChangedPath);
			}

		return formatter.toString();
	}
}
